﻿using IRepository;
using IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using YL.Core.Entity;
using YL.Core.Entity.Meter;
using YL.Utils.Extensions;
using YL.Utils.Json;
using YL.Utils.Table;

namespace Services
{
    public class Meter_DeviceFailureDayServices : BaseServices<DeviceFailureDay>, IMeter_DeviceFailureDayServices
    {
        private readonly SqlSugarClient _client;

        public Meter_DeviceFailureDayServices(SqlSugarClient client, IMeter_DeviceFailureDayRepository repository) : base(repository)
        {
            _client = client;
        }

        public string PageList(Bootstrap.BootstrapParams bootstrap, long userId)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }

            var query = _client.Queryable<DeviceFailureDay, DeviceInfo, ClientInfo,AllLevelUserClientInfoView>
                ((s, c, u,w) => new object[] {
                   JoinType.Left,s.DeviceId==c.DeviceId,
                   JoinType.Left,c.ClientId==u.ClientId,
                   JoinType.Left,u.ClientId==w.ClientId
                 })
                 .Where((s,c,u,w)=>s.Date>= Convert.ToDateTime(bootstrap.datemin) && s.Date <= Convert.ToDateTime(bootstrap.datemax) && w.UserId == userId)
                 .GroupBy((s,c,u)=>u.ClientId)
                 .Select((s,c,u) => new
                 {
                     ClientId = u.ClientId.ToString(),
                     ClientName=SqlFunc.AggregateMax(u.ClientName),
                     DeviceNum=SqlFunc.AggregateDistinctCount(s.DeviceId),
                     FailureCount = SqlFunc.AggregateSum(s.FailureCount),
                     DurationMin =SqlFunc.AggregateSum(s.DurationMin),
                     ConfirmTime = SqlFunc.AggregateSum(s.ConfirmTime),
                     DateMin = bootstrap.datemin,
                     DateMax = bootstrap.datemax,
                 })
                 .MergeTable().OrderBy((s)=>s.FailureCount,OrderByType.Desc);

            if (!bootstrap.search.IsEmpty())
            {
                query.Where((s) => s.ClientName.Contains(bootstrap.search));
            }
            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }

        public string Echart(Bootstrap.BootstrapParams bootstrap, long userId)
        {
            var query = _client.Queryable
                (
                _client.Queryable<DeviceFailureDay, DeviceInfo, ClientInfo, AllLevelUserClientInfoView>
                ((s, c, u, w) => new object[] {
                   JoinType.Left,s.DeviceId==c.DeviceId,
                   JoinType.Left,c.ClientId==u.ClientId,
                   JoinType.Left,u.ClientId==w.ClientId
                 })
                 .Where((s, c, u,w) => s.Date >= Convert.ToDateTime(bootstrap.datemin) && s.Date <= Convert.ToDateTime(bootstrap.datemax) && w.UserId == userId)
                 .GroupBy((s, c, u) => u.ClientId)
                 .Select((s, c, u) => new DeviceFailureDay()
                 {
                     ClientName = SqlFunc.AggregateMax(u.ClientName),
                     FailureCount = SqlFunc.AggregateSum(s.FailureCount)
                 })
                 ).ToList();
            return query.JilToJson();
        }

        public string DetailPageList(Bootstrap.BootstrapParams bootstrap,Int64 ClientId)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }

            var query = _client.Queryable<DeviceFailureDay, DeviceInfo>
                ((s, c) => new object[] {
                   JoinType.Left,s.DeviceId==c.DeviceId
                 })
                 .Where((s, c) => s.Date >= Convert.ToDateTime(bootstrap.datemin) && s.Date <= Convert.ToDateTime(bootstrap.datemax) && c.ClientId==ClientId )
                 .GroupBy((s, c) => c.DeviceId)
                 .Select((s, c) => new
                 {
                     DeviceId = c.DeviceId,
                     DeviceName = SqlFunc.AggregateMax(c.DeviceName),
                     FailureCount = SqlFunc.AggregateSum(s.FailureCount),
                     DurationMin = SqlFunc.AggregateSum(s.DurationMin),
                     ConfirmTime = SqlFunc.AggregateSum(s.ConfirmTime)
                 })
                 .MergeTable().OrderBy((s) => s.FailureCount, OrderByType.Desc);

            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }

        public string DetailEchart(Bootstrap.BootstrapParams bootstrap, Int64 ClientId)
        {
            var query = _client.Queryable
                (_client.Queryable<DeviceFailureDay, DeviceInfo>
                ((s, c) => new object[] {
                   JoinType.Left,s.DeviceId==c.DeviceId
                 })
                 .Where((s, c) => s.Date >= Convert.ToDateTime(bootstrap.datemin) && s.Date <= Convert.ToDateTime(bootstrap.datemax) && c.ClientId == ClientId)
                 .GroupBy((s, c) => c.DeviceId)
                 .Select((s, c) => new DeviceFailureDay()
                 {
                     DeviceName = SqlFunc.AggregateMax(c.DeviceName),
                     FailureCount = SqlFunc.AggregateSum(s.FailureCount)
                 })
                 ).ToList();
            return query.JilToJson();
        }
    }
}
